﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiografController;
using BiografController.ServiceReference;
using System.Globalization;

namespace BiografGUI
{
    public partial class ForestillingGUI : Form
    {
        ForestillingGUI gui;
        FilmController filmCtr;
        SalController salCtr;
        ForestillingController fCtr;

        private DateTime startDato;
        private DateTime slutDato;
        private string start;
        private string slut;
        private int film;
        private int salNr;
        private int datoDifferens;

        public ForestillingGUI()
        {
            this.gui = this;
            InitializeComponent();
            filmCtr = new FilmController();
            salCtr = new SalController();
            fCtr = new ForestillingController();
            PopulateFilmComboBox();
            PopulateStartComboBox();
            dateTimePicker2.Enabled = false;
        }

        private void PopulateFilmComboBox()
        {
            List<Film> filmListe = filmCtr.AlleFilm();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = filmListe;
            filmComboBox.DataSource = bindingSource.DataSource;
            filmComboBox.DisplayMember = "Titel";
            filmComboBox.ValueMember = "Titel";
            
        }

        private void PopulateSalComboBox(bool status)
        {
            List<Sal> salListe = salCtr.Alle3DSal(status);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = salListe;
            salComboBox.DataSource = bindingSource.DataSource;
            salComboBox.DisplayMember = "salNr";
            salComboBox.ValueMember = "salNr";
        }

        private void PopulateStartComboBox()
        {
            List<String> start = new List<string>();
            start.Add("15:00");
            start.Add("18:00");
            start.Add("21:00");
            this.startComboBox.DataSource = start;
        }



        private void filmComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (filmComboBox.SelectedItem != null)
            {
                // FilmId hentes fra filmComboBox.
                film = filmCtr.GetEnFilmFraTitel(filmComboBox.SelectedValue.ToString()).FilmID;
                resultRichTextBox.Text += "Valgt film:\t\t" + filmComboBox.SelectedValue + "\n";
                PopulateSalComboBox(filmCtr.GetEnFilm(film).Er3D);
            }
        }

        private void startComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (startComboBox.SelectedItem != null)
            {
                start = startComboBox.SelectedValue.ToString();
                resultRichTextBox.Text += "Valgt start tid:\t" + startComboBox.SelectedValue + "\n";
                
                if (startComboBox.SelectedValue.ToString().Equals("15:00"))
                {
                    slut = "17:00";
                }
                if (startComboBox.SelectedValue.ToString().Equals("18:00"))
                {
                    slut = "20:00";
                }
                if (startComboBox.SelectedValue.ToString().Equals("21:00"))
                {
                    slut = "23:00";
                }
                resultRichTextBox.Text += "Slut tid:\t\t" + slut + "\n";
            }
        }

        private void salComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (salComboBox.SelectedItem != null)
            {
                salNr = salCtr.GetEnSal(Convert.ToInt32(salComboBox.SelectedValue.ToString())).SalNr;
                resultRichTextBox.Text += "Valgt sal nummer:\t" + salNr + "\n";
            }
        }

        private void sletButton_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            resultRichTextBox.Text = "";
            startDato = DateTime.MinValue;
            slutDato = DateTime.MinValue;
            start = "";
            slut = "";
            film = 0;
            salNr = 0;
            dateTimePicker2.Enabled = false;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            startDato = dateTimePicker1.Value.Date;
            
            resultRichTextBox.Text += "Valgt start dato:\t" + startDato.ToShortDateString() + "\n";
            dateTimePicker2.Enabled = true;
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            slutDato = dateTimePicker2.Value.Date;
            DateDifference();
            int dage = datoDifferens + 1;
            resultRichTextBox.Text += "Valgt slut dato:\t" + slutDato.ToShortDateString() + 
                "\nHvor mange dage kører forestillingen: " + dage + "\n";
        }

        // needs to be worked on, only works if the dates lie in the same month.
        private void DateDifference()
        {
            datoDifferens = Convert.ToInt32(slutDato.Subtract(startDato).TotalDays);
        }

        private void opretButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            int maximum = datoDifferens + 15;
            progressBar1.Maximum = maximum;
            while (i < datoDifferens+1)
            {
                string strDate = startDato.ToShortDateString();
                int forestillingId = 0;
                forestillingId = fCtr.InsertForestilling(Convert.ToDateTime(strDate), start, slut, film, salNr);
                i++;
                
                startDato = startDato.AddDays(1);

                for (int raekkeNr = 0; raekkeNr < 3; raekkeNr++)
                {
                    for (int saedeNr = 0; saedeNr < 5; saedeNr++)
                    {
                        salCtr.NySaede(saedeNr+1, raekkeNr+1, forestillingId);
                        // progressbar needs to be worked on.
                        //progressBar1.Value++;
                    }
                }
            }
            Clear();
            MessageBox.Show("Forestillingerne er blevet oprettet");
            gui.Hide();
        }
    }
}
